package avicit.bdp.dds.server.worker.task.sqoop.generator;

import avicit.bdp.dds.dispatch.task.sqoop.SqoopParameters;
import avicit.bdp.dds.server.entity.TaskExecutionContext;
import avicit.bdp.dds.server.worker.task.sqoop.generator.sources.HdfsSourceGenerator;
import avicit.bdp.dds.server.worker.task.sqoop.generator.sources.HiveSourceGenerator;
import avicit.bdp.dds.server.worker.task.sqoop.generator.sources.MysqlSourceGenerator;
import avicit.bdp.dds.server.worker.task.sqoop.generator.targets.HdfsTargetGenerator;
import avicit.bdp.dds.server.worker.task.sqoop.generator.targets.HiveTargetGenerator;
import avicit.bdp.dds.server.worker.task.sqoop.generator.targets.MysqlTargetGenerator;

/**
 * Sqoop Job Scripts Generator
 */
public class SqoopJobGenerator {

    private static final String MYSQL = "MYSQL";
    private static final String HIVE = "HIVE";
    private static final String HDFS = "HDFS";

    /**
     * target script generator
     */
    private ITargetGenerator targetGenerator;
    /**
     * source script generator
     */
    private ISourceGenerator sourceGenerator;
    /**
     * common script generator
     */
    private CommonGenerator commonGenerator;

    public SqoopJobGenerator(){
        commonGenerator = new CommonGenerator();
    }

    private void createSqoopJobGenerator(String sourceType,String targetType){
        sourceGenerator = createSourceGenerator(sourceType);
        targetGenerator = createTargetGenerator(targetType);
    }

    /**
     * get the final sqoop scripts
     * @param sqoopParameters
     * @return
     */
    public String generateSqoopJob(SqoopParameters sqoopParameters, TaskExecutionContext taskExecutionContext){
        createSqoopJobGenerator(sqoopParameters.getSourceType(),sqoopParameters.getTargetType());
        if(sourceGenerator == null || targetGenerator == null){
            return null;
        }

        return commonGenerator.generate(sqoopParameters)
                + sourceGenerator.generate(sqoopParameters,taskExecutionContext)
                + targetGenerator.generate(sqoopParameters,taskExecutionContext);
    }

    /**
     * get the source generator
     * @param sourceType
     * @return
     */
    private ISourceGenerator createSourceGenerator(String sourceType){
        switch (sourceType){
            case MYSQL:
                return new MysqlSourceGenerator();
            case HIVE:
                return new HiveSourceGenerator();
            case HDFS:
                return new HdfsSourceGenerator();
            default:
                return null;
        }
    }

    /**
     * get the target generator
     * @param targetType
     * @return
     */
    private ITargetGenerator createTargetGenerator(String targetType){
        switch (targetType){
            case MYSQL:
                return new MysqlTargetGenerator();
            case HIVE:
                return new HiveTargetGenerator();
            case HDFS:
                return new HdfsTargetGenerator();
            default:
                return null;
        }
    }
}
